//
//  CRNewsHomeCollectionViewCell.m
//  NewAge
//
//  Created by dllo on 15/11/27.
//  Copyright © 2015年 ColorRevolution. All rights reserved.
//

#import "CRNewsHomeCollectionViewCell.h"
#import "Carousel.h"
#import "CRHeaderCell.h"
#import "CRNewsModel.h"
#import "CRHeaderModel.h"
#import "CRNewsTableViewCell.h"
#import "CRSpecialNewsTableViewCell.h"
#import "CRPicTableViewCell.h"

@interface CRNewsHomeCollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;


@end

@implementation CRNewsHomeCollectionViewCell



#pragma mark - init

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createViews];
    }
    return self;
}


#pragma mark - setter

- (void)setCurrentArr:(NSMutableArray *)currentArr {
    
    if (_currentArr != currentArr) {
        _currentArr = currentArr;
    }
    
    [self.headCollectionView reloadData];
    [self.tableView reloadData];
}



#pragma mark - createViews

- (void)createViews {
    
    self.tableView = [[UITableView alloc] init];
    _tableView.separatorStyle = NO;
    _tableView.contentInset = UIEdgeInsetsMake(200 * HEIGHT / 667.0, 0, 0, 0);
//    _tableView.bounces = NO;
    _tableView.contentOffset = CGPointMake(0, -200 * HEIGHT / 667.0);
    [self.contentView addSubview:_tableView];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    // tableView注册cell
    [_tableView registerClass:[CRNewsTableViewCell class] forCellReuseIdentifier:@"nReuse"];
    [_tableView registerClass:[CRSpecialNewsTableViewCell class] forCellReuseIdentifier:@"snReuse"];
    [_tableView registerClass:[CRPicTableViewCell class] forCellReuseIdentifier:@"pReuse"];
    
    self.headFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    _headFlowLayout.minimumInteritemSpacing = 0;
    _headFlowLayout.minimumLineSpacing = 0;
    _headFlowLayout.itemSize = CGSizeMake(WIDTH, 200 * HEIGHT / 667.0);
    _headFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.headCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, - 200 * HEIGHT / 667.0, WIDTH, 200 * HEIGHT / 667.0) collectionViewLayout:_headFlowLayout];
    _headCollectionView.contentSize = CGSizeMake(WIDTH * 6, 0);
    _headCollectionView.pagingEnabled = YES;
    [self.tableView addSubview:_headCollectionView];
    
    [_headCollectionView registerClass:[CRHeaderCell class] forCellWithReuseIdentifier:@"hReuse"];
    
    _headCollectionView.dataSource = self;
    _headCollectionView.delegate = self;
}



#pragma mark - layoutSubviews

// 设置位置
- (void)layoutSubviews {
//    [_headCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
//        Top(self.contentView.mas_top, 0);
//        Bottom(_tableView.mas_top, 0);
//        Left(self.contentView, 0);
//        Right(self.contentView, 0);
//        HequalNum(200 * HEIGHT / 667.0);
//    }];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        Top(self.contentView.mas_top, 0);
        Left(self.contentView, 0);
        Right(self.contentView, 0);
        Bottom(self.contentView, 0);
    }];
}



#pragma mark - collectionViewDateSource & Delegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.currentArr[0] ads].count + 3;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CRHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"hReuse" forIndexPath:indexPath];
    
    if (indexPath.row == 0) {
        
        cell.news = [[self.currentArr[0] ads] lastObject];
    }
    
    else if (indexPath.row == 1) {
        cell.news = self.currentArr[0];
    }
    
    else if (indexPath.row == [self.currentArr[0] ads].count + 2) {
        cell.news = self.currentArr[0];
    }
    
    else {
        cell.news = [self.currentArr[0] ads][indexPath.row - 2];
    }
    
    // 设置显示页数
    if (indexPath.row > 0 && indexPath.row < [self.currentArr[0] ads].count + 2) {
        NSString *page = [NSString stringWithFormat:@"%ld / %ld", indexPath.row, [self.currentArr[0] ads].count + 1];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:page];
        [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 1)];
        [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 1)];
        cell.pageLabel.attributedText = attributedString;
    }
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 1) {
        CRNewsModel *news = self.currentArr[0];
        
        
        if ([news.skipType isEqualToString:@"special"]) {
            [self.delegate pushToSpecialDetailWithStr:news.specialID title:news.title];
        }
        else {
            if (news.imgextra) {
                [self.delegate pushToPicDetailWithStr:news.photosetID title:news.title];
            }
            else {
                [self.delegate pushToDetailWithStr:news.url_3w title:news.title];
            }
        }
    }
    else if (indexPath.row > 1 && indexPath.row < [self.currentArr[0] ads].count + 2) {
        CRHeaderModel *header = [self.currentArr[0] ads][indexPath.row - 2];
        [self.delegate pushToPicDetailWithStr:header.myurl title:header.title];
//        if ([news.skipType isEqualToString:@"special"]) {
//            [self.delegate pushToSpecialDetailWithStr:news.specialID title:news.title];
//        }
//        else {
//            if (news.imgextra) {
//                [self.delegate pushToPicDetailWithStr:news.photosetID title:news.title];
//            }
//            else {
//                [self.delegate pushToDetailWithStr:news.url_3w title:news.title];
//            }
//        }
    }
}

#pragma mark - tableViewDataSource & Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.currentArr.count - 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.currentArr[indexPath.row + 1] imgextra]) {
        return 124 * HEIGHT / 667.0;
    }
    else {
        return 94 * HEIGHT / 667.0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CRNewsModel *news = self.currentArr[indexPath.row + 1];
    
    if ([news.skipType isEqualToString:@"special"]) {
        CRSpecialNewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"snReuse" forIndexPath:indexPath];
        cell.news = news;
        return cell;
    }
    else {
        if (news.imgextra) {
            CRPicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pReuse" forIndexPath:indexPath];
            cell.news = news;
            return cell;
        }
        else {
            CRNewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"nReuse" forIndexPath:indexPath];
            cell.news = news;
            return cell;
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CRNewsModel *news = self.currentArr[indexPath.row + 1];
    
    if ([news.skipType isEqualToString:@"special"]) {
        [self.delegate pushToSpecialDetailWithStr:news.specialID title:news.title];
    }
    else {
        if (news.imgextra) {
            [self.delegate pushToPicDetailWithStr:news.photosetID title:news.title];
        }
        else {
            [self.delegate pushToDetailWithStr:news.url_3w title:news.title];
        }
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.headCollectionView) {
        if (scrollView.contentOffset.x == WIDTH * ([self.currentArr[0] ads].count + 2)) {
            scrollView.contentOffset = CGPointMake(WIDTH, 0);
            NSLog(@"%ld, %g", [self.currentArr[0] ads].count + 3, scrollView.contentOffset.x);
        }
        else if (scrollView.contentOffset.x == 0) {
            scrollView.contentOffset = CGPointMake(WIDTH * ([self.currentArr[0] ads].count + 1), 0);
            NSLog(@"%ld, %g", [self.currentArr[0] ads].count + 3, scrollView.contentOffset.x);
        }
    }
}

@end
